﻿<template>
  <a-modal
    title="新增任务步骤"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="任务策略ID" v-show="false" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入任务策略ID" v-decorator="['policyTaskId']" />
        </a-form-item>
        <a-form-item label="标题" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input
            placeholder="请输入标题"
            v-decorator="['title', { rules: [{ required: true, message: '请输入标题！' }] }]"
          />
        </a-form-item>
        <a-form-item label="操作类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select
            style="width: 100%"
            placeholder="请选择操作类型"
            v-decorator="['actionType', { rules: [{ required: true, message: '请选择操作类型！' }] }]"
          >
            <a-select-option v-for="(item, index) in actionTypeData" :key="index" :value="item.code">{{
              item.name
            }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="步骤序号" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input-number
            placeholder="请输入步骤序号"
            style="width: 100%"
            v-decorator="['stepNo', { rules: [{ required: true, message: '请输入步骤序号！' }] }]"
          />
        </a-form-item>
        <a-form-item label="备份类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select
            style="width: 100%"
            placeholder="请选择备份类型"
            v-decorator="['backupType', { rules: [{ required: true, message: '请选择备份类型！' }] }]"
          >
            <a-select-option v-for="(item, index) in backupTypeData" :key="index" :value="item.code">{{
              item.name
            }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="压缩备份" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-switch v-decorator="['isCompressBuckup', { valuePropName: 'checked' }]" />
        </a-form-item>
        <a-form-item label="创建远程备份" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-switch v-decorator="['isRemoteCopy', { valuePropName: 'checked' }]" />
        </a-form-item>
        <a-form-item label="月备份" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-switch v-decorator="['isMonthly', { valuePropName: 'checked' }]" />
        </a-form-item>
        <a-form-item label="摘要说明" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入摘要说明" v-decorator="['description']" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { PolicyStepAdd } from '@/api/modular/main/PolicyStepManage'

export default {
  data() {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 15 },
      },
      actionTypeData: [],
      backupTypeData: [],
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this),
    }
  },
  methods: {
    // 初始化方法
    add(taskId) {
      this.visible = true
      const actionTypeOption = this.$options
      this.actionTypeData = actionTypeOption.filters['enumData']('ActionType')
      const backupTypeOption = this.$options
      this.backupTypeData = backupTypeOption.filters['enumData']('BackupType')
      setTimeout(() => {
        this.form.setFieldsValue({
          policyTaskId: taskId
        })
      }, 100)
    },
    /**
     * 提交表单
     */
    handleSubmit() {
      const {
        form: { validateFields },
      } = this
      this.confirmLoading = true
      validateFields((errors, values) => {
        if (!errors) {
          for (const key in values) {
            // WARN: 有可能导致类型错误，注意
            if (values[key] !== null && typeof values[key] === 'object') {
              values[key] = JSON.stringify(values[key])
            }
          }
          PolicyStepAdd(values)
            .then((res) => {
              if (res.success) {
                this.$message.success('新增成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error('新增失败：' + JSON.stringify(res.message))
              }
            })
            .finally((res) => {
              this.confirmLoading = false
            })
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel() {
      this.form.resetFields()
      this.visible = false
    },
  },
}
</script>
